// import pathUtil from "path-browserify";
//console.log('in nslUtils')
/**
 * 克隆一个对象
 * @param {boolean} deep 是否深度克隆
 */
export const clone = function (obj: any, deep: boolean) {
    if (Array.isArray(obj)) {
        if (deep) {
            //深度克隆
            var newArr:any = [] ;
            for (var i = 0; i < obj.length; i++) {
                newArr.push(clone(obj[i] , deep));
            }
            return newArr;
        }
        else {
            return obj.slice(); //复制数组
        }
    }
    else if (typeof obj === "object") {
        var newObj :any= {};
        for (var prop in obj) {
            if (deep) {
                //深度克隆
                newObj[prop] = clone(obj[prop], deep);
            }
            else {
                newObj[prop] = obj[prop];
            }
        }
        return newObj;
    }
    else {
        //函数、原始类型
        return obj; //递归的终止条件
    }
}


/**
 * 判断是否是外链
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
    const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
    return isExternal;
}

function mapToJson(map:Map<string,any>) {
    var obj = Object.create(null);
    //@ts-expect-error
    map.keys().forEach((key:string)=>{
        obj[key] = map.get(key);
    });
    return JSON.stringify(obj);
}

function toNumber(str:string|undefined|null):number{
    if(str === undefined || str === null ){
        return 0
    }
    if(str.trim() === ''){
        return 0;
    }
    if(str.includes('.')){
        return parseFloat(str);
    }else{
        return parseInt(str);
    }
}

function toInt(str:string|undefined|null):number{
    if(str === undefined || str === null ){
        return 0
    }
    if(str.trim() === ''){
        return 0;
    }
    return parseInt(str)
}

function toFloat(str:string|undefined|null):number{
    if(str === undefined || str === null ){
        return 0
    }
    if(str.trim() === ''){
        return 0;
    }
    return parseFloat(str)
}

function toBoolean(p:any):boolean{
    if(p){
        return true;
    }else{
        return false;
    }
}

function delayMs (duration:number){
    return new Promise((resolve:Function) => {
        setTimeout(() => {
          resolve();
        }, duration);
      });
}

function isNumber(value:any){
    let regPos=/^[0-9]+.?[0-9]*/;
    if(regPos.test(value+"")){
        return true;
    }else{
        return false;
    }
}

/**
 * 解析路径 如果是
 *
 * @param routePath 路由路径
 */
export function resolvePath(basePath:string,currentPath: string) {
    if (isExternal(currentPath)) {
      return currentPath;
    }
    let fullPath;
    const lastString=basePath.substring(basePath.length-1);
    if(lastString==="/"){
        fullPath=basePath+currentPath;
    }else{
        fullPath=basePath+"/"+currentPath;
    }
    return fullPath;
}

/**
 * 
 * @param val 将传入的参数转换成 带有%号的数据，
 * 如果不是数字的话，返回undefined
 */
export function toPercent(val:any,spaces:number){
    if(val === undefined || val === null ){
        return val;
    }
    if(spaces<2){
        spaces=2;
    }
    if(isNumber(val)){
        val=val+"";

        let num=toNumber(val)*100;
        let stringNum=num.toFixed(spaces-2)+"%";
        return stringNum;
    }
    return undefined;
}
/**
 * 将传入对象属性中的，数字转换成 百分比字符串
 * @param obj 
 * @param spaces 保留小数
 * @param deep 是否改变祖先
 */
export function objProps2Percent(obj:object,spaces:number,deep:boolean=false){
    if(obj instanceof Object){
        const keys=Object.keys(obj) as Array<keyof typeof obj>
        keys.forEach((key)=>{
            if (obj.hasOwnProperty(key) || (deep && !obj.hasOwnProperty(key))) { // 确保属性是对象自身的而不是从原型链继承的
                let val=obj[key];
                if(isNumber(val)){
                    obj[key]=toPercent(val,spaces) as never;
                }
            }
        })
    }
}

export function isKey<T extends object>(x:T,k: keyof T):k is keyof T{
    return k in x;
}

/**
 * obj => map
 * @param obj 
 * @returns 
 */
export function obj2Map(obj:object):Map<string,unknown>{
    let map=new Map<string,unknown>();
    if(obj instanceof Object){
        const keys=Object.keys(obj) as Array<keyof typeof obj>
        keys.forEach((key)=>{
            if (obj.hasOwnProperty(key)) { // 确保属性是对象自身的而不是从原型链继承的
                let val=obj[key];
                map.set(key,val);
            }
        })
    }
    return map;
}


const NslUtils={
    clone,
    isExternal,
    mapToJson,
    toNumber,
    toInt,
    toFloat,
    toBoolean,
    delayMs,
    isNumber
}



export {
    NslUtils,
    mapToJson,
}